package net.xdclass.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.model.CouponRecordDO;
import net.xdclass.model.LoginUser;
import net.xdclass.request.LockCouponRecordRequest;
import net.xdclass.service.CouponRecordService;
import net.xdclass.until.JsonData;
import net.xdclass.vo.CouponRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 二当家小D
 * @since 2022-02-08
 */
@RestController
@RequestMapping("/api/coupon_record/v1")
public class CouponRecordController {
    @Autowired
    private CouponRecordService couponRecordService;

    @ApiOperation("分页查询个人优惠券")
    @GetMapping("page")
    public JsonData page(
            @ApiParam("当前页数") @RequestParam(value = "page", defaultValue = "1") int page
            , @ApiParam("每页多少条") @RequestParam(value = "size", defaultValue = "20") int size) {

        Map<String, Object> page1 = couponRecordService.page(page, size);
        return JsonData.buildSuccess(page1);
    }

    @ApiOperation("查询优惠券记录详情")
    @GetMapping("detail/{record_id}")
    public JsonData getCouponRecordDetail(@PathVariable("record_id") long recordId) {
        CouponRecordVO couponRecordVO = couponRecordService.findById(recordId);
        return couponRecordVO == null ? JsonData.buildResult(BizCodeEnum.COUPON_NO_EXITS) : JsonData.buildSuccess(couponRecordVO);
    }

    @ApiOperation("rpc-锁定,优惠券记录")
    @PostMapping("lock_records")
    public JsonData lockCouponRecords(
            @ApiParam("锁定优惠券记录")
            @RequestBody LockCouponRecordRequest recordRequest) {
        JsonData jsonData = couponRecordService.lockCouponRecords(recordRequest);
        return jsonData;
    }
}

